package com.hjt.zhibo.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import jodd.util.StringUtil;

import com.hjt.zhibo.model.PlatFormRoom;
import com.jfinal.plugin.activerecord.Page;

public class PlatFormRoomTag extends SimpleTagSupport {
	
	private int pageNum;
	private int pageSize;
	private String platform;
	private String auth;
	private String roomType;
	private String roomName;
	private String key;
	
	public void doTag() throws JspException, IOException {
		
		JspContext JspContext = this.getJspBody().getJspContext();
		
		StringBuilder sql = new StringBuilder(" from platform_room t where 1=1 ");
		List<Object> values = new ArrayList<Object>();
		
		if(StringUtil.isNotBlank(platform)){
			sql.append(" and t.platform = ? ");
			values.add(platform);
		}
		
		if(StringUtil.isNotBlank(auth)){
			sql.append(" and t.auth = ? ");
			values.add(auth);
		}
		
		if(StringUtil.isNotBlank(roomType)){
			sql.append(" and t.room_type = ? ");
			values.add(roomType);
		}

		if(StringUtil.isNotBlank(roomName)){
			sql.append(" and t.room_name = ? ");
			values.add(roomName);
		}
		
		if(StringUtil.isNotBlank(key)){
			sql.append(" and (t.room_name like ? or t.auth like ? or t.room_type =? )");
			values.add("%" + key + "%");
			values.add("%" + key + "%");
			values.add("%" + key + "%");
		}
		
		sql.append(" order by t.count desc ");
		Object[] arrs = new Object[values.size()];
		arrs = values.toArray(arrs);
		
		Page<PlatFormRoom> page = PlatFormRoom.dao.paginate(pageNum, pageSize, "select * ", sql.toString(), arrs);
		if(page.getTotalRow() > 0) {
			for (PlatFormRoom platFormRoom : page.getList()) {
				 JspContext.setAttribute("room_url", platFormRoom.get("url"));
				 JspContext.setAttribute("room_pic", platFormRoom.get("pic"));
				 JspContext.setAttribute("room_room_name", platFormRoom.get("room_name"));
				 JspContext.setAttribute("room_platform", platFormRoom.get("platform"));
				 JspContext.setAttribute("room_room_type", platFormRoom.get("room_type"));
				 JspContext.setAttribute("room_auth", platFormRoom.get("auth"));
				 JspContext.setAttribute("room_count_txt", platFormRoom.get("count_txt"));
				 this.getJspBody().invoke(null);
			}
		} else{
			JspWriter jspWriter = this.getJspBody().getJspContext().getOut();
			jspWriter.append("<div class='search_title'>未找到");
			jspWriter.append("<strong>"+ key + roomType +"</strong>");
			jspWriter.append("的相关结果</div>");
			jspWriter.flush();
		}
		
	}
	
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public void setPlatform(String platform) {
		this.platform = platform;
	}
	
	public void setAuth(String auth) {
		this.auth = auth;
	}
	
	public void setRoomType(String roomType) {
		this.roomType = roomType;
	}
	
	public void setRoomName(String roomName) {
		this.roomName = roomName;
	}
	
	public void setKey(String key) {
		this.key = key;
	}

}
